package com.googlecode.javatools.classparser.impl;

import java.io.DataInput;

import com.googlecode.javatools.classparser.ConstantParser;
import com.googlecode.javatools.classparser.ConstantPoolParser;
import com.googlecode.javatools.classparser.types.ConstantEntry;
import com.googlecode.javatools.classparser.types.ConstantPool;
import com.googlecode.javatools.classparser.types.FileHeaderInfo;
import com.googlecode.javatools.classparser.types.impl.ConstantPoolImpl;

public class ConstantPoolParserImpl implements ConstantPoolParser
{
	private final ConstantParser constantParser;

	public ConstantPoolParserImpl(final ConstantParser constantParser)
	{
		this.constantParser = constantParser;
	}

	public ConstantPool parse(final DataInput dataInput, final FileHeaderInfo fileHeaderInfo) throws Exception
	{
		final ConstantEntry[] constantPool = new ConstantEntry[fileHeaderInfo.constantPoolSize()];

		for (int i = 1; i < fileHeaderInfo.constantPoolSize(); i++)
		{
			final ConstantEntry constant = constantParser.parseNextConstant(dataInput);

			constantPool[i] = constant;

			if (constantParser.isEightByteConstantThatUsesTwoPoolEntries(constant))
			{
				i++;
			}
		}

		return new ConstantPoolImpl(constantPool);
	}

}
